using Engine;
using Game;

namespace RuthlessConquest {
    public class SettingsScreen : Screen {
        public ButtonWidget NicknameButton;
        public BevelledButtonWidget FactionButton;
        public RectangleWidget FactionRectangle;
        public SliderWidget DefaultFleetStrengthSlider;
        public BevelledButtonWidget ShowFlagsButton;
        public SliderWidget SfxSlider;
        public SliderWidget MusicSlider;
        public ButtonWidget ModSettingsButton;
        public ButtonWidget ResetButton;
        public ButtonWidget BackButton;
        public const string fName = "RCSettingsScreen";

        public SettingsScreen() {
            Widget[] obj = [new BackgroundWidget(), new BoidsWidget(), new InterlaceWidget(), null];
            StackPanelWidget panel1 = new() {
                Direction = LayoutDirection.Vertical,
                Margin = new Vector2(20f, 0f),
                Children = {
                    new CanvasWidget { Size = new Vector2(0f, 10f) },
                    new CanvasWidget {
                        Size = new Vector2(float.PositiveInfinity, -1f),
                        Children = {
                            new LabelWidget {
                                Text = LanguageControl.Get(fName, "1"),
                                FontScale = 0.75f,
                                Color = Colors.Fore,
                                HorizontalAlignment = WidgetAlignment.Center
                            }
                        }
                    },
                    new CanvasWidget { Size = new Vector2(0f, 10f) }
                }
            };
            CanvasWidget obj3 = new() {
                Size = new Vector2(float.PositiveInfinity, float.PositiveInfinity), Children = { new RectangleWidget(), new InterlaceWidget() }
            };
            ScrollPanelWidget scrollPanel = new() { Direction = LayoutDirection.Vertical, Margin = new Vector2(2f, 2f) };
            StackPanelWidget panel2 = new() { Direction = LayoutDirection.Vertical };
            UniformSpacingPanelWidget obj6 = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "2"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            NicknameButton = new BevelledButtonWidget {
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f)
            };
            obj6.Children.Add(NicknameButton);
            panel2.Children.Add(obj6);
            UniformSpacingPanelWidget obj8 = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "3"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            StackPanelWidget obj9 = new() {
                Margin = new Vector2(20f, 0f), HorizontalAlignment = WidgetAlignment.Near, VerticalAlignment = WidgetAlignment.Center
            };
            CanvasWidget canvasWidget = new();
            FactionButton = new BevelledButtonWidget { Size = new Vector2(float.PositiveInfinity, 60f) };
            canvasWidget.Children.Add(FactionButton);
            FactionRectangle = new RectangleWidget {
                Size = new Vector2(32f, 32f),
                FillColor = Color.Black,
                OutlineColor = Color.Transparent,
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Center
            };
            canvasWidget.Children.Add(FactionRectangle);
            obj9.Children.Add(canvasWidget);
            obj8.Children.Add(obj9);
            panel2.Children.Add(obj8);
            UniformSpacingPanelWidget obj12 = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "4"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            DefaultFleetStrengthSlider = new SliderWidget { MinValue = 0.2f, MaxValue = 1f, Granularity = 0.1f, Margin = new Vector2(20f, 0f) };
            obj12.Children.Add(DefaultFleetStrengthSlider);
            panel2.Children.Add(obj12);
            UniformSpacingPanelWidget obj14 = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "5"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            ShowFlagsButton = new BevelledButtonWidget {
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f)
            };
            obj14.Children.Add(ShowFlagsButton);
            panel2.Children.Add(obj14);
            UniformSpacingPanelWidget obj16 = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "6"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            SfxSlider = new SliderWidget { MinValue = 0f, MaxValue = 1f, Granularity = 0.1f, Margin = new Vector2(20f, 0f) };
            obj16.Children.Add(SfxSlider);
            panel2.Children.Add(obj16);
            UniformSpacingPanelWidget obj18 = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "7"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            MusicSlider = new SliderWidget { MinValue = 0f, MaxValue = 1f, Granularity = 0.1f, Margin = new Vector2(20f, 0f) };
            obj18.Children.Add(MusicSlider);
            panel2.Children.Add(obj18);
            UniformSpacingPanelWidget obj19 = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "9"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            ModSettingsButton = new BevelledButtonWidget {
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f)
            };
            obj19.Children.Add(ModSettingsButton);
            panel2.Children.Add(obj19);
            UniformSpacingPanelWidget obj20 = new() {
                Margin = new Vector2(0f, 3f),
                Children = {
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "11"),
                        HorizontalAlignment = WidgetAlignment.Far,
                        VerticalAlignment = WidgetAlignment.Center,
                        Margin = new Vector2(20f, 0f)
                    }
                }
            };
            ResetButton = new BevelledButtonWidget {
                Text = LanguageControl.Get(fName, "12"),
                Margin = new Vector2(20f, 0f),
                HorizontalAlignment = WidgetAlignment.Near,
                VerticalAlignment = WidgetAlignment.Center,
                Size = new Vector2(float.PositiveInfinity, 60f)
            };
            obj20.Children.Add(ResetButton);
            panel2.Children.Add(obj20);
            scrollPanel.Children.Add(panel2);
            obj3.Children.Add(scrollPanel);
            panel1.Children.Add(obj3);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, 20f) });
            BackButton = new BevelledButtonWidget {
                Text = LanguageControl.Back, Size = new Vector2(200f, 60f), Sound = "RCSounds/Click2", HorizontalAlignment = WidgetAlignment.Center
            };
            panel1.Children.Add(BackButton);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, 10f) });
            obj[3] = panel1;
            Children.Add(obj);
            scrollPanel.ScrollPosition = 0f;
        }

        public override void Update() {
            if (NicknameButton.IsClicked) {
                NamesManager.ShowSetPlayerNameDialog(this, null);
            }
            if (FactionButton.IsClicked) {
                DialogsManager.ShowDialog(
                    this,
                    new FactionSelectionDialog(
                        delegate(object o) {
                            if (o != null) {
                                SettingsManager.Faction = (Faction)o;
                            }
                        }
                    )
                );
            }
            if (DefaultFleetStrengthSlider.IsSliding) {
                SettingsManager.DefaultFleetStrength = DefaultFleetStrengthSlider.Value;
            }
            if (ShowFlagsButton.IsClicked) {
                SettingsManager.ShowFlags = !SettingsManager.ShowFlags;
            }
            if (ModSettingsButton.IsClicked) {
                ScreensManager.SwitchScreen("RCModSettings");
            }
            if (SfxSlider.IsSliding) {
                global::Game.SettingsManager.SoundsVolume = SfxSlider.Value;
            }
            if (MusicSlider.IsSliding) {
                global::Game.SettingsManager.MusicVolume = MusicSlider.Value;
            }
            if (ResetButton.IsClicked) {
                DialogsManager.ShowDialog(
                    this,
                    new MessageDialog(
                        LanguageControl.Warning,
                        LanguageControl.Get(fName, "13"),
                        LanguageControl.Yes,
                        LanguageControl.No,
                        button => {
                            if (button == MessageDialogButton.Button1) {
                                SettingsManager.Initialize();
                            }
                        }
                    )
                );
            }
            if (Input.Back
                || BackButton.IsClicked) {
                ScreensManager.SwitchScreen("RCMainMenu");
            }
            NicknameButton.Text = string.IsNullOrEmpty(SettingsManager.PlayerName) ? LanguageControl.Get(fName, "8") : SettingsManager.PlayerName;
            FactionButton.CenterColor = Player.GetColor(SettingsManager.Faction);
            FactionButton.BevelColor = Player.GetColor(SettingsManager.Faction);
            FactionRectangle.Subtexture = new Subtexture(Ship.GetTexture(SettingsManager.Faction), Vector2.Zero, Vector2.One);
            DefaultFleetStrengthSlider.Value = SettingsManager.DefaultFleetStrength;
            DefaultFleetStrengthSlider.Text = $"{SettingsManager.DefaultFleetStrength * 100f:0}%";
            ShowFlagsButton.Text = SettingsManager.ShowFlags ? LanguageControl.Yes : LanguageControl.No;
            ModSettingsButton.Text = string.Format(LanguageControl.Get(fName, "10"), SettingsManager.LobbyServers.Count);
            SfxSlider.Value = global::Game.SettingsManager.SoundsVolume;
            SfxSlider.Text = $"{(global::Game.SettingsManager.SoundsVolume * 100f):0}%";
            MusicSlider.Value = global::Game.SettingsManager.MusicVolume;
            MusicSlider.Text = $"{(global::Game.SettingsManager.MusicVolume * 100f):0}%";
        }
    }
}